package com.mooc.ppjoke.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.mooc.lib_ui_common.widget.bottom.AppBottomBar
import com.mooc.ppjoke.R
import com.mooc.ppjoke.widget.NavGraphBuilder

class FrameActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
    private var navController: NavController? = null
    private var navView: AppBottomBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_frame)
        initView()
    }

    private fun initView(){
        navView = findViewById(R.id.frame_nav_bottom)
        val fragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
        navController = NavHostFragment.findNavController(fragment!!)
        NavGraphBuilder.build(this, fragment.childFragmentManager, navController!!, fragment.id)
        navView!!.setOnNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        navController!!.navigate(item.getItemId())
        return !TextUtils.isEmpty(item.getTitle())
    }

}